package com.mycloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * Cross   Origin    Resource   Sharing
 *
 * Created by PC on 2019/1/21.
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfig = new CorsConfiguration();
       //是否支持cookie跨域
        corsConfig.setAllowCredentials(true);
        //原始域
        corsConfig.setAllowedOrigins(Arrays.asList("*"));//http://a.com
        //头
        corsConfig.setAllowedHeaders(Arrays.asList("*"));
        //哪些方法,GET,POST.....
        corsConfig.setAllowedMethods(Arrays.asList("*"));
        //缓存时间,这个时间段内，对相同的跨域请求，不再做处理
        corsConfig.setMaxAge(300L);
        source.registerCorsConfiguration("/**", corsConfig);

        return new CorsFilter(source);
    }
}
